package com.ckd.flyingtrip.charge.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.activity.BaseActivity;
import com.ckd.flyingtrip.charge.javabean.OvertimeOrderBean;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 * 订单信息
 * */
public class OrderInformationActivity extends BaseActivity {


    @BindView(R.id.toolber_color_title)
    TextView title;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_volage)
    TextView tvVolage;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.ll_charge)
    LinearLayout llCharge;
    @BindView(R.id.tv_overtime)
    TextView tvOvertime;
    @BindView(R.id.tv_overtime_money)
    TextView tvOvertimeMoney;
    @BindView(R.id.ll_overtime)
    LinearLayout llOvertime;

    //机柜码
    private String content;

    //订单信息
    private String orderMoney;  //待支付金额
    private String orderTime;    //超时时间
    private String orderType;  //电池类型 ( 0 磷酸铁锂电池、1 铅酸电池)
    private String orderV;     //电池电压 ( 48、60、72)

    //取电池
    private String takeCode; //  1柜门已打开 2超时 0开门失败
    private String takeMsg;

    //超时支付
    private String userId;
    private String money;  //待支付金额
    private String overtime; //超时时间
    private String batteryType; //电池类型 ( 0 磷酸铁锂电池、1 铅酸电池)
    private String batteryV; // 48、60、72
    private String cabinetDoor; //柜门
    private String cabinet; //换电柜编码
    private String overtimeMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_information);
        ButterKnife.bind(this);

        initView();

    }

    private void initView() {
        title.setText("订单信息");

        Intent intent = getIntent();
        orderMoney = intent.getStringExtra("orderMoney");
        orderTime = intent.getStringExtra("orderTime");
        orderType = intent.getStringExtra("orderType");
        orderV = intent.getStringExtra("orderV");

        if ("0".equals(orderType)) {
            tvType.setText("磷酸铁锂电池");
        } else if ("1".equals(orderType)) {
            tvType.setText("铅酸电池");
        }

        if ("1".equals(orderV)) {
            tvVolage.setText("48V");
        } else if ("2".equals(orderV)) {
            tvVolage.setText("60V");
        } else if ("3".equals(orderV)) {
            tvVolage.setText("72V");
        }

        if (null == tvOvertimeMoney) {
            llCharge.setVisibility(View.VISIBLE);
            tvTime.setText("");
            tvMoney.setText("");
        } else {
            llOvertime.setVisibility(View.VISIBLE);
            tvOvertime.setText(orderTime + "小时");
            tvOvertimeMoney.setText("¥" + orderMoney);
        }

    }

    //取电池
    private void initTakeBattery() {
        final Gson mGson = new Gson();
        String url = Constants.takeBattery;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("取电池", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    takeCode = json.getString("code");
                    takeMsg = json.getString("msg");
                    if (takeCode.equals("2")) {
                        OvertimeOrderBean bean = new Gson().fromJson(s, OvertimeOrderBean.class);
                        userId = bean.getData().getUserId();
                        money = bean.getData().getMoney();
                        overtime = bean.getData().getOvertime();
                        batteryType = bean.getData().getBatteryType();
                        batteryV = bean.getData().getBatteryV();
                        cabinetDoor = bean.getData().getCabinetDoor();
                        cabinet = bean.getData().getCabinet();
                        overtimeMsg = bean.getMsg();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("cabinet", "123");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    /*
     * 跳转二维码界面
     * */
    private void tzErweima() {
        Intent intent = new Intent(OrderInformationActivity.this, CaptureActivity.class);
        ZxingConfig config = new ZxingConfig();
        config.setShowAlbum(false);
        intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
        startActivityForResult(intent, Constants.ZXINGE_MAIN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 扫描二维码/条码回传
        if (requestCode == Constants.ZXINGE_MAIN && resultCode == RESULT_OK) {
            if (data != null) {
                //content所需机柜码
                content = data.getStringExtra(Constant.CODED_CONTENT);
                initTakeBattery();

                if (takeCode.equals("1")) {
                    toast(takeMsg);
                } else if (takeCode.equals("2")) {
                    Intent intent1 = new Intent(OrderInformationActivity.this, OvertimePaymentActivity.class);
                    intent1.putExtra("userId", userId);
                    intent1.putExtra("money", money);
                    intent1.putExtra("overtime", overtime);
                    intent1.putExtra("batteryType", batteryType);
                    intent1.putExtra("batteryV", batteryV);
                    intent1.putExtra("cabinetDoor", cabinetDoor);
                    intent1.putExtra("cabinet", cabinet);
                    intent1.putExtra("overtimeMsg", overtimeMsg);
                    startActivityForResult(intent1,2000);
                } else if (takeCode.equals("0")) {
                    toast(takeMsg);
                }
            }
        }
        if (requestCode ==2000 && resultCode==2001){
            finish();
        }
    }

    @OnClick({R.id.toolber_color_back, R.id.rl_take})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolber_color_back:
                finish();
                break;
            case R.id.rl_take:
                tzErweima();
                break;
        }
    }
}
